package com.ren.fm.controller;


import com.ren.utils.page.PageItem;
import com.ren.fm.entity.ReclFm;
import com.ren.fm.entity.vo.ReclFmQueryVo;
import com.ren.fm.entity.vo.ReclFmVo;
import com.ren.fm.service.ReclFmService;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author RZY
 * @since 2022-07-13
 */

@Api(tags = "FM数据后台接口")
@RestController
@RequestMapping("/recl-fm")
public class ReclFmController {

    @Autowired
    ReclFmService fmService;

    @ApiOperation(value = "查询指定id的FM数据")
    @GetMapping("/{id}")
    public R getFmById(@PathVariable Long id) {
        ReclFmVo fm = fmService.getFmVoById(id);
        return R.ok()
                .data("fm", fm == null ? new ReclFm() : fm);
    }

    @ApiOperation(value = "查询FM数据分页")
    @PostMapping("/{current}/{limit}")
    public R getFmOfPage(@RequestBody(required = false) ReclFmQueryVo reclFmQueryVo,
                         @PathVariable long current,
                         @PathVariable long limit) {
        PageItem<ReclFmVo> fmPageItem = fmService.getFmOfPage(reclFmQueryVo, current, limit);
        return R.ok()
                .data("records", fmPageItem.getPageRecords())
                .data("total", fmPageItem.getTotal());
    }

    @ApiOperation(value ="修改指定id的FM数据")
    @PutMapping("")
    public R updateFmById(@RequestBody ReclFm reclFm) {
        boolean res = fmService.updateFmById(reclFm);
        if(res) return R.ok()
                .message("FM修改成功！");
        else return R.error()
                .message("FM修改失败，请稍后再试！");
    }

    @ApiOperation(value = "删除指定id的FM数据")
    @DeleteMapping("/{id}")
    public R delFmById(@PathVariable Long id) {
        boolean res = fmService.delFmById(id);
        if(res) return R.ok()
                .message("FM删除成功！");
        else return R.error()
                .message("FM删除失败，请稍后再试！");
    }

    @ApiOperation(value = "添加FM数据")
    @PostMapping("")
    public R addFmById(@RequestBody ReclFm reclFm) {
        boolean res = fmService.addFm(reclFm);
        if(res) return R.ok()
                .message("FM添加成功！");
        else return R.error()
                .message("FM添加失败，请稍后再试！");
    }
}

